package com.ct.utils;

import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;

public class JDBCUtils {
    public static DataSource ds;
    static {
        Properties ps=new Properties();
        InputStream resourceAsStream = JDBCUtils.class.getClassLoader().getResourceAsStream("mysql.properties");
        try {
            ps.load(resourceAsStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            DataSource ds1= DruidDataSourceFactory.createDataSource(ps);
            if (ds1==null){
                throw new Exception("获取数据源失败");
            }else {
                ds=ds1;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    public static Connection getConnection() throws SQLException {
        return ds.getConnection();
    }

    public static DataSource getDataSource(){
        System.out.println(ds);
        return ds;
    }
}
